/**
 * 详情对话框
 */
var FlowGoodsInfoDlg = {
    data: {
        goodsId: "",
        goodsName: "",
        goodsTypeId: "",
        note: "",
        cover:""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;



    //普通图片上传
    var upload = layui.upload;
    upload.render({
        elem: '#coverBtn'
        ,url: Feng.ctxPath + '/system/upload' //改成您自己的上传接口
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#filePreview').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            $("#cover").val("/upload/"+res.data.fileName);
            Feng.success(res.message);
        }
        ,error: function(){
            Feng.error("上传文件失败！");
        }
    });


    //类型
    var goodTypeAjax = new $ax(Feng.ctxPath + "/flowGoods/goodsType");
    var goodTypeResult = goodTypeAjax.start();
    if(goodTypeResult.code == 200){
        var goodTypeStr = "<option value=''>请选择物品分类</option>";
        for (var i = 0;i<goodTypeResult.data.length;i++){
            goodTypeStr += "<option value='"+goodTypeResult.data[i].goodsTypeId+"'>"+goodTypeResult.data[i].typeName+"</option>";
        }
        $("#goodsTypeId").html(goodTypeStr);
        form.render('select');
    }






    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/flowGoods/detail?goodsId=" + Feng.getUrlParam("goodsId"));
    var result = ajax.start();
    form.val('flowGoodsForm', result.data);

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        data.field.goodsName = escape(data.field.goodsName);
        var ajax = new $ax(Feng.ctxPath + "/flowGoods/editItem", function (data) {
            Feng.success("更新成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

});